<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="username">
      <el-input v-model="dataForm.username" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="密码" prop="password">
      <el-input v-model="dataForm.password" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item label="昵称" prop="nickname">
      <el-input v-model="dataForm.nickname" placeholder="昵称"></el-input>
    </el-form-item>
    <el-form-item label="手机号码" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="手机号码"></el-input>
    </el-form-item>
    <el-form-item label="微信open_id" prop="openId">
      <el-input v-model="dataForm.openId" placeholder="微信open_id"></el-input>
    </el-form-item>
    <el-form-item label="性别：0->未知；1->男；2->女" prop="gender">
      <el-input v-model="dataForm.gender" placeholder="性别：0->未知；1->男；2->女"></el-input>
    </el-form-item>
    <el-form-item label="头像" prop="icon">
      <el-input v-model="dataForm.icon" placeholder="头像"></el-input>
    </el-form-item>
    <el-form-item label="生日" prop="birthday">
      <el-input v-model="dataForm.birthday" placeholder="生日"></el-input>
    </el-form-item>
    <el-form-item label="用户来源" prop="sourceType">
      <el-input v-model="dataForm.sourceType" placeholder="用户来源"></el-input>
    </el-form-item>
    <el-form-item label="积分" prop="integration">
      <el-input v-model="dataForm.integration" placeholder="积分"></el-input>
    </el-form-item>
    <el-form-item label="成长值" prop="growth">
      <el-input v-model="dataForm.growth" placeholder="成长值"></el-input>
    </el-form-item>
    <el-form-item label="帐号启用状态:0->禁用；1->启用" prop="status">
      <el-input v-model="dataForm.status" placeholder="帐号启用状态:0->禁用；1->启用"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="注册时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          username: '',
          password: '',
          nickname: '',
          phone: '',
          openId: '',
          gender: '',
          icon: '',
          birthday: '',
          sourceType: '',
          integration: '',
          growth: '',
          status: '',
          createTime: ''
        },
        dataRule: {
          username: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          password: [
            { required: true, message: '密码不能为空', trigger: 'blur' }
          ],
          nickname: [
            { required: true, message: '昵称不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '手机号码不能为空', trigger: 'blur' }
          ],
          openId: [
            { required: true, message: '微信open_id不能为空', trigger: 'blur' }
          ],
          gender: [
            { required: true, message: '性别：0->未知；1->男；2->女不能为空', trigger: 'blur' }
          ],
          icon: [
            { required: true, message: '头像不能为空', trigger: 'blur' }
          ],
          birthday: [
            { required: true, message: '生日不能为空', trigger: 'blur' }
          ],
          sourceType: [
            { required: true, message: '用户来源不能为空', trigger: 'blur' }
          ],
          integration: [
            { required: true, message: '积分不能为空', trigger: 'blur' }
          ],
          growth: [
            { required: true, message: '成长值不能为空', trigger: 'blur' }
          ],
          status: [
            { required: true, message: '帐号启用状态:0->禁用；1->启用不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/user/userinfo/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.username = data.userinfo.username
                this.dataForm.password = data.userinfo.password
                this.dataForm.nickname = data.userinfo.nickname
                this.dataForm.phone = data.userinfo.phone
                this.dataForm.openId = data.userinfo.openId
                this.dataForm.gender = data.userinfo.gender
                this.dataForm.icon = data.userinfo.icon
                this.dataForm.birthday = data.userinfo.birthday
                this.dataForm.sourceType = data.userinfo.sourceType
                this.dataForm.integration = data.userinfo.integration
                this.dataForm.growth = data.userinfo.growth
                this.dataForm.status = data.userinfo.status
                this.dataForm.createTime = data.userinfo.createTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/user/userinfo/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'username': this.dataForm.username,
                'password': this.dataForm.password,
                'nickname': this.dataForm.nickname,
                'phone': this.dataForm.phone,
                'openId': this.dataForm.openId,
                'gender': this.dataForm.gender,
                'icon': this.dataForm.icon,
                'birthday': this.dataForm.birthday,
                'sourceType': this.dataForm.sourceType,
                'integration': this.dataForm.integration,
                'growth': this.dataForm.growth,
                'status': this.dataForm.status,
                'createTime': this.dataForm.createTime
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
